package com.cloud.school.bus.teacherhelper.modules.stuclass;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.android.support.jhf.utils.MobileUtils;
import com.android.support.jhf.utils.ToolUtils;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.base.activity.BaseActivity;
import com.cloud.school.bus.teacherhelper.base.view.TitleNavBarView;
import com.cloud.school.bus.teacherhelper.entitys.Student;
import com.cloud.school.bus.teacherhelper.modules.settings.AboutActivity;
import com.cloud.school.bus.teacherhelper.modules.stuclass.adapter.AddStudentExistAdapter;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.AddStudentRequest;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.AddStudentResponse;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.InClassRequest;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.InClassResponse;

/**
 * 增加学生，已经存在的学生列表
 * 
 * @author jiahongfei
 * 
 */
public class AddStudentExistActivity extends BaseActivity {

	private ListView mListView;
	private AddStudentExistAdapter mAddStudentExistAdapter;
	private Button mAddClassButton;

	private String mNameString;
	private String mParentPhoneString;
	private List<Student> mStudents = new ArrayList<Student>();;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_student_exist);

		init();
	}

	@Override
	protected void init() {

		mNameString = getIntent().getStringExtra("name");
		mParentPhoneString = getIntent().getStringExtra("parentPhone");
		mStudents = (List<Student>) getIntent().getSerializableExtra("StudentList");

		mAddClassButton = (Button) findViewById(R.id.addClassButton);
		mAddClassButton.setEnabled(false);
		mListView = (ListView) findViewById(R.id.listView);
		mAddStudentExistAdapter = new AddStudentExistAdapter(mContext, mStudents);
		addListViewHead();
		mListView.setAdapter(mAddStudentExistAdapter);

		setTitleNavBar();
		setListener();
	}

	private void addListViewHead() {
		int padding = ToolUtils.dipToPx(mContext, 10);
		TextView headTextView = new TextView(mContext);
		headTextView.setText(getString(R.string.please_select_the_corrert_one));
		headTextView.setPadding(padding, 0, padding, 0);
		mListView.addHeaderView(headTextView);

		TextView footerTextView = new TextView(mContext);
		footerTextView.setGravity(Gravity.CENTER);
		footerTextView.setText(getString(R.string.please_call_400_606_3996));
		footerTextView.setPadding(padding, 0, padding, 0);
		mListView.addFooterView(footerTextView);
	}

	@Override
	protected void setListener() {
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (0 == arg2) {
					return;
				}
				if (mAddStudentExistAdapter.getCount() + 1 == arg2) {
					MobileUtils.callPhoneActivity(mContext, AboutActivity.ABOUT_PHONE, Intent.ACTION_VIEW);
					return;
				}
				boolean selected = mAddStudentExistAdapter
						.setSelectedIndex(arg2 - 1);
				if (selected) {
					mAddClassButton.setEnabled(true);

				} else {
					mAddClassButton.setEnabled(false);
				}
			}
		});
		mAddClassButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Student student = (Student) mAddStudentExistAdapter.getItem(mAddStudentExistAdapter.getCurrentItem());
				inClass(student);
				
			}
		});
		Button newStudentButton = (Button) findViewById(R.id.newStudentButton);
		boolean showNewStudent = getIntent().getBooleanExtra("ShowNewStudent", false);
		if(showNewStudent){
			newStudentButton.setVisibility(View.VISIBLE);
		}else {
			newStudentButton.setVisibility(View.GONE);
		}
		newStudentButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext,
						AddStudentDetailInfoActivity.class);
				intent.putExtra("name", mNameString);
				intent.putExtra("parentPhone", mParentPhoneString);
				startActivityForResult(intent, 0);
			}
		});
	}

	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage(getString(R.string.add_student));
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 0: {
			// 添加学生返回
			switch (resultCode) {
			case RESULT_OK: {
				// 添加成功返回
				setResult(RESULT_OK);
				finish();
				break;
			}
			case RESULT_CANCELED: {

				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
	}
	
	/**
	 * 选择的学生加入班级
	 */
	private void inClass(Student student) {
		final String type = "0";
		NetworkClient.getNetworkClient().GetRequest(
				new InClassRequest(mContext, student.studentid, student.student_uid, type),
				new InClassResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if ("1".equals(code)) {
							if ("1".equals(type)) {
								// 离班
								HandlerToastUI.getHandlerToastUI(
										mContext, "离班成功");
							} else if ("0".equals(type)) {
								// 调班
//								HandlerToastUI.getHandlerToastUI(
//										mContext, "调班成功");
								HandlerToastUI.getHandlerToastUI(
										mContext, getString(R.string.add_successfully));
								mApplication.mStudentList.add(student);
								setResult(RESULT_OK);
								finish();
							}
						} else if ("-3".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
									"studentid 为空");
						} else if ("-5".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
									"studentuid 为空");
						} else if ("-2".equals(code)) {
							if ("1".equals(type)) {
								// 离班
								HandlerToastUI.getHandlerToastUI(
										mContext, "该学生不在本班级");
							} else if ("0".equals(type)) {
								// 调班
								HandlerToastUI.getHandlerToastUI(
										mContext, "该学生已在本班级");
							}

						}
					}
				});
	}
	
}
